package define

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

var Response = &response{}

type response struct {
	Code    int         `json:"code"`
	Msg     string      `json:"msg"`
	Data    interface{} `json:"data"`
	Success bool        `json:"success"`
}

// ResultSuc @Title 返回成功
func (r *response) ResultSuc(c *gin.Context, msg string, data interface{}) {
	c.JSON(http.StatusOK, response{
		Code: 0,
		Msg:  msg,
		Data: data,
	})
}

// ResultSuc @Title 返回成功
func (r *response) ResultSuccess(c *gin.Context, msg string, data interface{}) {
	c.JSON(http.StatusOK, response{
		Success: true,
		Code:    0,
		Msg:     msg,
		Data:    data,
	})
}

// ResultSuc @Title 返回成功无数据
func (r *response) ResultSucc(c *gin.Context, msg string) {
	c.JSON(http.StatusOK, response{
		Code: 0,
		Msg:  msg,
	})
}

// ResultFail @Title 返回失败
func (r *response) ResultFail(c *gin.Context, code int, msg string, data ...interface{}) {
	if len(data) > 0 {
		c.JSON(http.StatusOK, response{
			Code: code,
			Msg:  msg,
			Data: data[0],
		})
	} else {
		c.JSON(http.StatusOK, response{
			Code: code,
			Msg:  msg,
			Data: nil,
		})
	}
}

type responseTotal struct {
	Code  int         `json:"code"`
	Msg   string      `json:"msg"`
	Data  interface{} `json:"data"`
	Total int64       `json:"count"`
}

// ResultSucLayUI  @Title 返回成功
func (r *response) ResultSucLayUI(c *gin.Context, msg string, data interface{}, total int64) {
	c.JSON(http.StatusOK, responseTotal{
		Code:  0,
		Msg:   msg,
		Data:  data,
		Total: total,
	})
}
